package com.sky.common.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author toki
 * 自定义redis配置类
 */
@Configuration
@Slf4j
//@ConditionalOnClass(RedisConnectionFactory.class)
@AutoConfigureBefore(RedisAutoConfiguration.class)  // 在springboot的自动配置redis之前执行自定义的配置，防止重复Bean
public class RedisConfiguration {

//    @Bean(name = "CustomRedisTemplate")
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        log.info("开始创建redis模板对象");
        // 创建redis模板对象
        final RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        // 设置redis连接工厂对象,用于创建连接,操作redis数据库
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 设置key的字符串类型序列化器,防止乱码问题
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // 设置value的字符串类型序列化器,防止乱码问题
//        redisTemplate.setValueSerializer(new StringRedisSerializer());
        return redisTemplate;
    }
}
